/*
 * Copyright (C) 2011 Fabio Hufschmid at bfh.ch
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ch.bfh.aeneas.core.io.gesture;

import android.app.ListActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import ch.bfh.aeneas.R;
import ch.bfh.aeneas.exchange.intent.AeneasIntent;
import java.util.List;
import java.util.Set;

/**
 * Shows a little contextmenue that contains actions for createand delete gestures
 * @author Fabio Hufschmid at bfh.ch
 */
public class GestureBuilderContextMenu extends ListActivity {

    private static GestureLibrary sStore;
    private String mGestureName;
    private final String LOG_TAG = "AeneasGBContextMenu";
    private List<Gesture> mGestureList=null;
    private Resources res;
    private String[] mlistItems;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // ToDo add your GUI initialization code here        
        setContentView(R.layout.gb_context_menue_1);
        res=getResources();
        mlistItems=res.getStringArray(R.array.contextmenu_actions);
        setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, mlistItems));
        
        mGestureName=getIntent().getExtras().getString("SelectedElement");
        this.setTitle(R.string.gestureontextmenu_title);

        if (sStore == null) {
            sStore = GestureLibraries.fromFile("/mnt/sdcard/aeneas/gestures");
        }
        sStore.load();
        Log.i(LOG_TAG, "Request for starting data-service");
       
    }
    
    /**This methode sends the received itemid to the next class if the item new Gesture was choosen
     * if the item delete gesture was choosen, it deletes the actual gesture
     * 
     * {@inheritDoc}
     */
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        String item = Long.toString(getListAdapter().getItemId(position));
       
        //delete Gesture
        if(item.equals("1"))
        {
            //Log.i(LOG_TAG,"Selected Element: "+item);
            removeGesture(mGestureName);
        }
        //new gesture
        else if(item.equals("0"))
        {
            Intent mIntent = new Intent(this,CreateGesture.class);		
            mIntent.putExtra("ItemName", mGestureName);
            this.startActivity(mIntent);
        }
    }
    
    
    /**
     * This method delete a gesture with a specific name
     * 
     * @param inName The name of gesture which is to delete
     */
    public void removeGesture(String inName){
        mGestureList=sStore.getGestures(inName);
        if(mGestureList==null)
        {
            speech(res.getString(R.string.gestureontextmenu_no_gesture_to_del));
        }
        else if(mGestureList.size()==1)
        {
            sStore.removeGesture(inName, mGestureList.get(0));
            Log.i(LOG_TAG,"Gesture to delete: "+mGestureList.get(0));
            sStore.save();
            sStore.load();
            speech(res.getString(R.string.gestureontextmenu_gesture_del));
        }
        else
        {
            Log.i(LOG_TAG,"There are more then one gesture with the same name. Gestures found: "+mGestureList.size());
        }
        
    }
    
    /**returns the gesture store for other classes
     * 
     * @return gesture library for this application 
     */
    static GestureLibrary getStore() {
        return sStore;
    }
    
    /**Sends a broadcast for en intelligent TTS Output
     * 
     * @param inText the text which is to speak
     */
    public void speech(String inText)
    {
        Intent output = new Intent(AeneasIntent.ACTION_OUTPUT);
        output.putExtra("offAISpeech", inText);
        super.sendBroadcast(output);
    }
     
    /**Sends a broadcast for e primitve TTS Output
     * 
     * @param inText the text which is to speak
     */
     public void speechAiOn(String inText)
    {
        Intent output = new Intent(AeneasIntent.ACTION_OUTPUT);
        output.putExtra("Speech", inText);
        super.sendBroadcast(output);
    }
    

}